/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.infonova.education.microservice.controller;

import com.infonova.education.microservice.repository.entity.Order;
import com.infonova.education.microservice.repository.entity.OrderPosition;
import com.infonova.education.microservice.service.OrderService;
import com.itextpdf.text.DocumentException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @author Stefan
 */
@Controller
@RequestMapping(value = "/ui/cart")
public class OrderUIController {

    @Autowired
    private OrderService orderService;

    @RequestMapping(value = "", method = RequestMethod.GET)
    public String showCartUI(Model model) {
        model.addAttribute("order", new Order());
        return "show_cart_home";
    }

    @RequestMapping(value = "create")
    public String createCart(Order order, BindingResult orderBindingResult, Model model) {
        long orderId = orderService.createOrder(order);
        Order newOrder = orderService.findOrderById(orderId);
        model.addAttribute("order", newOrder);
        model.addAttribute("orderPosition", new OrderPosition());
        Long price = orderService.calculatePrice(orderId);
        model.addAttribute("price", price);
        return "show_order";
    }

    @RequestMapping(value = "show/{orderID}")
    public String showOrder(@PathVariable("orderID") long orderId, Model model) {
        Order order = orderService.findOrderById(orderId);
        model.addAttribute("order", order);
        model.addAttribute("orderPosition", new OrderPosition());
        Long price = orderService.calculatePrice(orderId);
        model.addAttribute("price", price);
        return "show_order";
    }

    @RequestMapping(value = "createPosition")
    public String showCreatePositionView(Model model) {
        model.addAttribute("orderPosition", new OrderPosition());
        return "createPosition";
    }

    @RequestMapping(value = "position")
    public String createPosition(OrderPosition position, BindingResult orderBindingResult, Model model) {
        //position.setOrderId(orderID);
        orderService.addPosition(position);
        Order order = orderService.findOrderById(position.getOrderId());
        model.addAttribute("order", order);
        Long price = orderService.calculatePrice(position.getOrderId());
        model.addAttribute("price", price);
        return "show_order";
    }

    @RequestMapping(value = "finish/{orderID}")
    public String finishOrder(@PathVariable("orderID") long orderId, Model model) {
        orderService.finishOrder(orderId);
        model.addAttribute("order", new Order());
        return "show_cart_home";
    }
    
    @RequestMapping(value = "delete/{positionId}")
    public String deletePosition(@PathVariable("positionId") long orderPositionId, Model model) {
        OrderPosition pos = orderService.findOrderPositionById(orderPositionId);
        model.addAttribute("order", pos.getOrder());
        orderService.deletePosition(pos);
        return "show_cart_home";
    }

}
